#import "MyPreferencesTable.h"
#import "MainView.h"

@implementation MyPreferencesTable
- (id)initWithFrame:(CGRect)rect {
    if ((self == [super initWithFrame:CGRectMake(rect.origin.x, rect.origin.y + NAV_BAR_HEIGHT, rect.size.width, rect.size.height - NAV_BAR_HEIGHT)]) != nil) {
        int i, j;

        for(i=0;i<NUM_GROUPS;i++) {
            groupcell[i] = NULL;
            for(j=0;j<CELLS_PER_GROUP;j++)
                cells[i][j] = NULL;
        }

        [ self setDataSource: self ];
        [ self setDelegate: self ];
    }

    return self;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)aTable {

    /* Number of logical groups, including labels */
    return NUM_GROUPS;
}

 - (int)preferencesTable:(UIPreferencesTable *)aTable
    numberOfRowsInGroup:(int)group
{
    switch (group) {
        case(0):
            return 3;
            break;
    }
}

- (UIPreferencesTableCell *)preferencesTable:
    (UIPreferencesTable *)aTable
    cellForGroup:(int)group
{
     if (groupcell[group] != NULL)
         return groupcell[group];

     groupcell[group] = [ [ UIPreferencesTableCell alloc ] init ];
     switch (group) {
         case (0):
             [ groupcell[group] setTitle: @"Settings" ];
             break;
     }
     return groupcell[group];
}

- (float)preferencesTable:(UIPreferencesTable *)aTable
    heightForRow:(int)row
    inGroup:(int)group
    withProposedHeight:(float)proposed
{
    /* Return height for group titles */
    if (row == -1) {
        if (group < 2)
            return 40;
    }

    /* Segmented controls are larger than the others */
    if (group == 0 && row == 2)
        return 55.0;

    return proposed;
}

- (BOOL)preferencesTable:(UIPreferencesTable *)aTable
    isLabelGroup:(int)group
{
    if (group == 2)
        return YES;
    return NO;
}

- (UIPreferencesTableCell *)preferencesTable:
    (UIPreferencesTable *)aTable
    cellForRow:(int)row
    inGroup:(int)group
{
    UIPreferencesTableCell *cell;

    if (cells[group][row] != NULL)
        return cells[group][row];

    cell = [ [ UIPreferencesTableCell alloc ] init ];
    [ cell setEnabled: YES ];

    switch (group) {
        case (0):
            switch (row) {
                case (0):
                    [ cell release ];
                    cell = [ [ UIPreferencesTextTableCell alloc ]
                        init ];
                    [ cell setTitle: @"Host" ];
                    [ cell setEnabled: YES ];
                    [ cell setValue: [prefs objectForKey:@"host"] ];
                    break;
                case (1):
                    [ cell release ];
                    cell = [ [ UIPreferencesTextTableCell alloc ]
                        init ];
                    [ cell setTitle: @"Port" ];
                    [ cell setEnabled: YES ];
                    [ cell setValue: [prefs objectForKey:@"port"] ];
                    break;
                case (2):
                    [ cell release ];
                    cell = [ [ UIPreferencesTextTableCell alloc ]
                        init ];
                    [ cell setTitle: @"Version" ];
                    [ cell setEnabled: NO ];
                    [ cell setValue: @"1.0.0" ];
                    break;
           }
           break;
        case (1):
            [cell setTitle:@"Settings will take effect on the next restart"];
            break;
    }

    [cell setShowSelection:NO];
    cells[group][row] = cell;
    return cell;
}
- (void) setPreferences:(NSDictionary *)_prefs {
	prefs = _prefs;
}

- (NSDictionary *) getPreferences {
	dolog("creating prefs");
	return [NSDictionary dictionaryWithObjectsAndKeys: 
            [cells[0][0] value], @"host", 
  		    [cells[0][1] value], @"port", 
  		    [cells[0][2] value], @"cell", nil];
}
@end
